package com.blb.zy_community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.zy_community.entity.ZyOwner;
import com.blb.zy_community.entity.ZyOwnerRoom;
import com.blb.zy_community.entity.ZyOwnerRoomRecord;
import com.blb.zy_community.mapper.ZyOwnerMapper;
import com.blb.zy_community.mapper.ZyOwnerRoomMapper;
import com.blb.zy_community.mapper.ZyOwnerRoomRecordMapper;
import com.blb.zy_community.mapper.ZyRoomMapper;
import com.blb.zy_community.service.IZyOwnerRoomRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 房屋绑定记录表  服务实现类
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@Service
public class ZyOwnerRoomRecordServiceImpl extends ServiceImpl<ZyOwnerRoomRecordMapper, ZyOwnerRoomRecord> implements IZyOwnerRoomRecordService {

    @Autowired
    private ZyOwnerRoomRecordMapper zyOwnerRoomRecordMapper;
    @Autowired
    private ZyOwnerMapper zyOwnerMapper;

    @Autowired
    private ZyOwnerRoomMapper zyOwnerRoomMapper;
    @Override
    public List<ZyOwnerRoomRecord> selectZyOwnerRoomRecord(Long ownerRoomId) {
        return zyOwnerRoomRecordMapper.selectZyOwnerRoomRecord(ownerRoomId);
    }

    @Transactional
    @Override
    public void updateRecord(String recordId, String recordAuditOpinion) {
        ZyOwnerRoomRecord ownerRoomRecord = zyOwnerRoomRecordMapper.selectOne(new QueryWrapper<ZyOwnerRoomRecord>().eq("record_id", recordId));
        System.out.println("审核："+ownerRoomRecord);
        ZyOwnerRoom zyOwnerRoom = zyOwnerRoomMapper.selectOne(new QueryWrapper<ZyOwnerRoom>().eq("owner_room_id", ownerRoomRecord.getOwnerRoomId()));
        ZyOwner zyOwner = zyOwnerMapper.selectOne(new QueryWrapper<ZyOwner>().eq("owner_id", ownerRoomRecord.getOwnerId()));
        ownerRoomRecord.setOwnerId(zyOwner.getOwnerId());
        ownerRoomRecord.setRoomStatus("Binding");
        zyOwnerRoom.setRoomStatus("Binding");
        zyOwnerRoomMapper.updateById(zyOwnerRoom);
        ownerRoomRecord.setRecordAuditOpinion(recordAuditOpinion);
        ownerRoomRecord.setUpdateTime(LocalDateTime.now());
        zyOwnerRoomRecordMapper.updateById(ownerRoomRecord);
    }

    @Transactional
    @Override
    public void turnDownRecord(String recordId, String recordAuditOpinion) {
        ZyOwnerRoomRecord ownerRoomRecord = zyOwnerRoomRecordMapper.selectOne(new QueryWrapper<ZyOwnerRoomRecord>().eq("record_id", recordId));
        System.out.println("审核："+ownerRoomRecord);
        ZyOwnerRoom zyOwnerRoom = zyOwnerRoomMapper.selectOne(new QueryWrapper<ZyOwnerRoom>().eq("owner_room_id", ownerRoomRecord.getOwnerRoomId()));
        ZyOwner zyOwner = zyOwnerMapper.selectOne(new QueryWrapper<ZyOwner>().eq("owner_id", ownerRoomRecord.getOwnerId()));
        ownerRoomRecord.setOwnerId(zyOwner.getOwnerId());
        ownerRoomRecord.setRoomStatus("Reject");
        zyOwnerRoom.setRoomStatus("Reject");
        ownerRoomRecord.setRecordAuditOpinion(recordAuditOpinion);
        ownerRoomRecord.setUpdateTime(LocalDateTime.now());
        zyOwnerRoomRecordMapper.updateById(ownerRoomRecord);
    }
}
